package com.example.user_manage.Component;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 *身份验证失败处理器
 */
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    /**
     * 登录失败时调用这个方法
     * @param request 请求对象
     * @param response 响应对象
     * @param exception 异常对象
     * @throws IOException IO异常
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request
            , HttpServletResponse response
            , AuthenticationException exception) throws IOException {
        //指定响应的支付及编码
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("登录失败");
    }
}
